package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    // 1. 增
    @PostMapping
    public Result add(@RequestBody Role role) {       // @RequestBody：将前端成传来的json封装为对象    多见于【增】【改】
        log.info("添加角色，参数：{}", role);
        roleService.add(role);
        return Result.success();
    }

    // 2. 删
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除角色: {}" , id);
        roleService.deleteById(id);
        return Result.success();
    }

    // 3. 改     结合--查--根据ID
    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("修改角色，参数：{}", role);
        roleService.update(role);
        return Result.success();
    }

    // 4. 查--分页查询
    @GetMapping
    public Result list(String name, String label,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询角色列表，参数：name={},label={},page={},pagesize={}", name, label, page, pageSize);
        PageResult<Role> pageResult =roleService.list(name, label, page, pageSize);
        return Result.success(pageResult);
    }

        // 查--根据ID
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询角色详情，参数：{}", id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }

        // 查--查所有角色
    @GetMapping("/list")
    public Result getAllRoles() {
        log.info("查询所有角色");
        return Result.success(roleService.getAllRoles());
    }
}
